package com.comleader.operator.controller;

import static com.comleader.operator.common.response.Result.success;

import java.io.IOException;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.comleader.operator.common.page.PageQuery;
import com.comleader.operator.common.response.Result;
import com.comleader.operator.pojo.entity.IovCardEntity;
import com.comleader.operator.pojo.entity.PersonalUserEntity;
import com.comleader.operator.pojo.entity.SmsPortEntity;
import com.comleader.operator.pojo.param.OperatorInspectParam;
import com.comleader.operator.service.IovCardService;
import com.comleader.operator.service.OperatorInspectService;
import com.comleader.operator.service.PersonalUserService;
import com.comleader.operator.service.SmsPortService;
import com.mybatisflex.core.paginate.Page;

import lombok.AllArgsConstructor;

/**
 * 运营商检查控制层。
 * 
 * @menu 运营商实名制检查
 * @author 连新景
 * @since 1.0
 */
@AllArgsConstructor
@Validated
@RestController
@RequestMapping("/operatorInspect")
public class OperatorInspectController {

    private final OperatorInspectService operatorInspectService;

    private final PersonalUserService personalUserService;

    private final IovCardService iovCardService;

    private final SmsPortService smsPortService;

    /**
     * @Description 导入检查数据
     * @Auhtor 连新景
     * @Date 2024/07/29 10:00
     * @param file 文件
     * @param businessType 业务类型(1-个人用户实名制 2-车联网卡实名制 3-短信端口业务)
     * @return java.lang.Integer 文件导入记录id
     */
    @PostMapping("/import")
    public Result<Integer> dataImport(@RequestParam("file") @NotNull(message = "文件不能为空") MultipartFile file,
        @RequestParam @NotNull(message = "业务类型不能为空") Integer businessType) throws IOException {
        return success(operatorInspectService.readFileAndSaveDb(file, businessType));
    }

    /**
     * 关闭
     *
     * @param businessType 业务类型
     * @return {@code true} 关闭成功，{@code false} 关闭失败
     */
    @DeleteMapping("/close")
    public void close(@RequestParam @NotNull(message = "业务类型不能为空") Integer businessType,
        @RequestParam @NotNull(message = "文件导入记录id不能为空") Integer importRecordId) {
        operatorInspectService.deleteImportData(businessType, importRecordId);
    }

    /**
     * 立即检查
     *
     * @param param
     * @return
     */
    @PostMapping("/inspect")
    public void inspect(@RequestBody @Valid OperatorInspectParam param) {
        operatorInspectService.inspect(param);
    }

    /**
     * 个人用户分页查询
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page/personalUser")
    public Page<PersonalUserEntity> personalUserPage(PageQuery page) {
        return personalUserService.page(PageQuery.build(page));
    }

    /**
     * 车联网卡分页查询
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page/iovCard")
    public Page<IovCardEntity> iovCardPage(PageQuery page) {
        return iovCardService.page(PageQuery.build(page));
    }

    /**
     * 短信端口分页查询
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page/smsPort")
    public Page<SmsPortEntity> smsPortPage(PageQuery page) {
        return smsPortService.page(PageQuery.build(page));
    }
}